package cn.client.ui.view.main.chat.message

import androidx.compose.animation.animateColorAsState
import androidx.compose.foundation.Image
import androidx.compose.foundation.border
import androidx.compose.foundation.clickable
import androidx.compose.foundation.layout.Arrangement
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.Spacer
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.fillMaxWidth
import androidx.compose.foundation.layout.height
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.runtime.setValue
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import cn.client.R
import cn.client.ui.view.main.chat.ChatList


/**
 * 发送者：头像位于右侧，即当前用户
 */
@Composable
fun SendMsg(
    msg: Message,
    modifier: Modifier = Modifier
) {
    var isExpanded by remember { mutableStateOf(false) }
    val sendMsgColor by animateColorAsState( if (isExpanded) MaterialTheme.colorScheme.primary else MaterialTheme.colorScheme.surface, label = "")

    Row(
        verticalAlignment = Alignment.CenterVertically,
        horizontalArrangement = Arrangement.SpaceBetween,
        modifier = Modifier.padding(all = 8.dp).fillMaxWidth()
    ) {
        Column(
            modifier = Modifier.weight(1f).clickable { isExpanded = !isExpanded }
        ) {
            /* TODO: name */
            Text(
                text = msg.author,
                modifier = Modifier.align(Alignment.End).padding(end = 8.dp),
                color = MaterialTheme.colorScheme.secondary,
                style = MaterialTheme.typography.titleSmall
            )
            // Add a vertical space between the author and message texts
            Spacer(modifier = Modifier.height(4.dp))
            /* TODO: message box */
            Surface(
                shape = MaterialTheme.shapes.medium,
                shadowElevation =  1.dp,
                color = sendMsgColor,
                modifier =  Modifier.align(Alignment.End).padding(end = 8.dp)
            ) {
                Text(
                    text = msg.body,
                    modifier = Modifier.padding(all = 4.dp),
                    maxLines = if (isExpanded) Int.MAX_VALUE else  1,
                    style = MaterialTheme.typography.bodyMedium,
                )
            }
        }
//        Spacer(modifier = Modifier.width(8.dp))
        /* TODO: avatar */
        Image(
            painter = painterResource(R.drawable.ab1_inversions),
            contentDescription = "Contact profile picture",
            contentScale = ContentScale.Crop,
            modifier = Modifier
                .size(40.dp)
                .clip(CircleShape)
                .border(1.dp, MaterialTheme.colorScheme.primary, CircleShape)
                .align(Alignment.CenterVertically) // Align the image to the end (right side)
        )
    }
}



data class Message(val author: String, val body: String)

@Preview(showBackground = true)
@Composable
fun sender(){
    Surface(){
        Column(
            Modifier.fillMaxSize()
        ){
//            SendMsg(
//                msg = Message("Kevin","Hey, take a look at Jetpack Compose, it's great! Hey, take a look at Jetpack Compose, it's great!")
//            )
//            ReceiveMsg(
//                msg = Message("Kevin","Hey, take a look at Jetpack Compose, it's great! Hey, take a look at Jetpack Compose, it's great!")
//            )
//
//            val messages: List<Message> = listOf(
//                Message("Kevin","Hello!"),
//                Message("Ada","Hello!"),
//                Message("Kevin","I'm Kevin."),
//                Message("Ada","I'm Ada."),
//                Message("Ada","Hey, take a look at Jetpack Compose, it's great!"),
//                Message("Kevin","Hey, take a look at Jetpack Compose, it's great!"),
//            )
//            ChatList(messages = messages)
        }

    }
}

